package cn.janyd.jmall.userweb.manager.passport;

import cn.janyd.common.core.vo.CommonResult;
import cn.janyd.jmall.userweb.controller.passport.vo.AccessTokenRespVo;
import cn.janyd.jmall.userweb.controller.passport.vo.PassportLoginBySmsReqVo;
import cn.janyd.jmall.userweb.converter.PassportConverter;
import cn.janyd.jmall.user.rpc.user.UserRpcService;
import cn.janyd.jmall.user.rpc.user.dto.UserRespDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author janyd
 * @since 2020/8/10 18:01
 */
@Service
public class PassportManager {

    @DubboReference(version = "${jmall.dubbo.versions.UserRpcService}")
    private UserRpcService userRpcService;

    public AccessTokenRespVo loginBySms(PassportLoginBySmsReqVo loginBySmsReqDTO, String ip) {

        //获取用户
        CommonResult<UserRespDTO> createUserResult = userRpcService.createUserIfAbsent(
                PassportConverter.INSTANCE.convert(loginBySmsReqDTO).setIp(ip));
        createUserResult.checkError();

        return new AccessTokenRespVo()
                .setAccessToken("xxxxx")
                .setRefreshToken("xxxxx")
                .setExpiresTime(LocalDateTime.now());
    }
}
